import datetime
import time

from django.shortcuts import render
from django.http import HttpResponse,JsonResponse

from order.models import Order
from util.token_tools import make_token,check_login

# 加载订单列表视图(无数据)
def get_list_view(request):
    return render(request,'order_list.html')

#根据状态查询订单列表 status:0 全部订单 1 未处理订单 2 处理中订单 3 完成订单 4 过期订单
@check_login
def reqeust_status_view(request,username,status):
    user = request.login_user
    if user.user_name != username:
        return JsonResponse({'code': 10102, 'error': '请重新登录'})
    else:
        order_list = Order.objects.filter(user= request.login_user)
        list = []
        for item  in order_list:
            # 当前时间时间戳 东八区
            loc_now_time_stamp = time.time()
            # 数据库取出时间
            sql_time = item.date_time
            sql_time_str = sql_time.strftime('%Y-%m-%d %H:%M:%S')
            sql_time = time.strptime(sql_time_str,'%Y-%m-%d %H:%M:%S')
            sql_time_stamp = time.mktime(sql_time)
            bj_sql_time_stamp = sql_time_stamp + 8 * 60 * 60
            # 转换东八区字符串
            bj_sql_time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(bj_sql_time_stamp))
            # 如果当前时间戳大于订单时间戳 订单失效
            print(f"status = {item.status}\n 当前日期 ={loc_now_time_stamp} 数据库日期 ={bj_sql_time_stamp}")
            if (item.status == 1) and (loc_now_time_stamp > bj_sql_time_stamp):
                item.status = 4
                item.save()
            status_str = deal_with_status(item.status)
            dict = {
                "model_img":str(item.order_model.model_img),
                "model_title":item.order_model.title,
                "order_code":item.order_code,
                "name":item.name,
                "content":item.content,
                "phone":item.phone,
                "price":item.price,
                "date_time":bj_sql_time_str,
                "status":status_str,
                "address":item.address,
                "status_val":item.status,
            }
            if status == 0:
                list.append(dict)
            elif status == 1:
                if item.status == 1:
                    list.append(dict)
            elif status == 2:
                if item.status == 2:
                    list.append(dict)
            elif status == 3:
                if item.status == 3:
                    list.append(dict)
            elif status == 4:
                if item.status == 4:
                    list.append(dict)

        res_dic = {
            'order_list': list
        }
        return JsonResponse({'code': 200, 'data': res_dic})


def deal_with_status(status_value):
    status_str = ""
    if status_value == 1:
        status_str = "未处理订单"
    elif status_value == 2:
        status_str = "订单处理中"
    elif status_value == 3:
        status_str = "订单完成"
    elif status_value == 4:
        status_str = "订单失效"
    return status_str;


#根据订单号删除订单列表
@check_login
def delete_view(request,username,order_code):
    user = request.login_user
    if user.user_name != username:
        return JsonResponse({'code': 10102, 'error': '请重新登录'})
    else:
        try:
            order = Order.objects.get(order_code=order_code)
            order.delete()
            return JsonResponse({'code': 200, 'msg': "删除成功!"})
        except:
            return JsonResponse({'code': 10101, 'error': "删除失败!"})

#根据状态查询订单列表并根据时间进行排序
@check_login
def orderby_view(request,orderby,status):
    if orderby == "asc":
        order_list = Order.objects.filter(user= request.login_user.id,status=status).order_by('date_time')
    elif orderby == "desc":
        order_list = Order.objects.filter(user= request.login_user.id,status=status).order_by('-date_time')
    res_dic = {
        'order_list': order_list
    }
    return JsonResponse({'code': 200, 'data': res_dic})